package com.atguigu.service_edu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.service_edu.entity.EduChapter;
import com.atguigu.service_edu.entity.chapter.ChapterVo;
import com.atguigu.service_edu.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author clark
 * @since 2020-04-10
 */
@Api(description = "课程大纲管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @ApiOperation(value = "根据课程ID获取课程大纲")
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(
            @ApiParam(value = "课程ID",name="courseId",required = true)
            @PathVariable String courseId
    ){
       List<ChapterVo> list  = eduChapterService.getChapterVideoByCourseId(courseId);


        return R.ok().data("allChapterVideo",list);
    }


    @ApiOperation(value = "添加课程大纲")
    @PostMapping("/addChapter")
    public R addChapter(
            @ApiParam(value = "课程大纲",name="chapter",required = false)
            @RequestBody
            EduChapter chapter
    ){
        eduChapterService.save(chapter);
        return R.ok();
    }

    @ApiOperation(value = "根据Id获取章节")
    @GetMapping("/getChapter/{chapterId}")
    public R getChapterInfo(
            @ApiParam(value="大纲id",name="chapterId",required = true)
            @PathVariable
            String chapterId
    ){
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("eduChapter",eduChapter);
    }

    @ApiOperation(value = "编辑课程大纲")
    @PutMapping("/updateChapter")
    public R updateChapter(
            @ApiParam(value = "课程大纲",name="chapter",required = false)
            @RequestBody
                    EduChapter chapter
    ){
        eduChapterService.updateById(chapter);
        return R.ok();
    }

    /**
     * 该章节下如果有小结,不可删除,抛出异常
     * 如果没有小结,可以删除
     * @param chapterId
     * @return
     */
    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean flag = eduChapterService.deleteChapter(chapterId);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }

    }






}

